package com.elitesland.system.rest;

import com.elitesland.core.base.ApiCode;
import com.elitesland.core.base.ApiResult;
import com.elitesland.core.base.PagingVO;
import com.elitesland.system.entity.SysRoleDO;
import com.elitesland.system.param.SysRoleNewParam;
import com.elitesland.system.param.SysRoleQueryParam;
import com.elitesland.system.param.SysRoleUpdateParam;
import com.elitesland.system.service.SysRolePermissionService;
import com.elitesland.system.service.SysRoleService;
import com.elitesland.system.service.SysUserService;
import com.elitesland.system.vo.SysPermissionVO;
import com.elitesland.system.vo.SysRoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * <pre>
 * [功能说明]
 * </pre>
 *
 * @author Mir
 * @date 2020/7/5
 */
@Slf4j
@RestController
@RequestMapping("/sys/roles")
@Api(value = "角色信息维护", tags = {"角色信息维护"})
@RequiredArgsConstructor
public class SysRoleController {

    private final SysRoleService sysRoleService;

    private final SysUserService sysUserService;

    private final SysRolePermissionService sysRolePermissionService;

    @PostMapping("/q")
    @ApiOperation("检索角色信息")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", responseContainer = "List", response = SysRoleDO.class)
    )
    public ApiResult<PagingVO<SysRoleVO>> search(@RequestBody SysRoleQueryParam param) {
        val ret = sysRoleService.search(param);
        return ApiResult.ok(ret);
    }

    @PostMapping
    @ApiOperation("创建角色信息")
    public ApiResult<Long> createInBatch(@RequestBody SysRoleNewParam role) {
        val ret = sysRoleService.create(role);
        return ApiResult.ok(ret);
    }

    @GetMapping("/{id}")
    @ApiOperation("通过ID获取单个角色信息")
    public ApiResult<SysRoleDO> one(@PathVariable Long id) {
        val roleOpt = sysRoleService.one(id);
        if (roleOpt.isEmpty()) {
            return ApiResult.fail(ApiCode.NOT_FOUND);
        }
        return ApiResult.ok(roleOpt.get());
    }

    @PutMapping
    @ApiOperation("更新角色信息")
    public ApiResult<Integer> update(@RequestBody SysRoleUpdateParam role) {
        sysRoleService.update(role);
        return ApiResult.ok(1);
    }

    @GetMapping("/all")
    @ApiOperation("列出所有角色")
    public ApiResult<Object> listAll() {
        val roles = sysRoleService.listAll();
        return ApiResult.ok(roles);
    }

    @GetMapping("/current/{username}")
    @ApiOperation("根据用户名返回对应的角色清单")
    public ApiResult<Object> listByUsername(@PathVariable String username) {
        val roles = sysUserService.listRolesByUsername(username);
        return ApiResult.ok(roles);
    }

    @PutMapping("/{id}")
    @ApiOperation("切换角色启用状态")
    public ApiResult<Boolean> switchUserStatus(@PathVariable Long id) {
        val ret = sysRoleService.switchRoleStatus(id);
        return ApiResult.ok(ret);
    }

    @GetMapping("/{id}/menuids")
    @ApiOperation("根据角色ID，返回授权的菜单ID集合")
    public ApiResult<List<String>> listMenuIdsById(@PathVariable Long id) {
        return ApiResult.ok(sysRoleService.listMenusById(id));
    }

    @GetMapping("/{id}/actionids")
    @ApiOperation("根据角色ID， 返回授权的能力ID集合")
    public ApiResult<List<String>> listActionIdsById(@PathVariable Long id) {
        return ApiResult.ok(sysRoleService.listActionsById(id));
    }

    @GetMapping("/{id}/menus/{menuid}")
    @ApiOperation("根据角色ID，和菜单ID，返回授权的能力清单")
    public ApiResult<Set<SysPermissionVO>> listActionsByMenuOfRole(
            @PathVariable Long id,
            @PathVariable Long menuId
    ) {
        return ApiResult.ok(sysRolePermissionService.listActionsByMenuIdOfRole(id, menuId));
    }

    @PostMapping("/{id}/menus/{menuid}")
    @ApiOperation("根据角色ID，和菜单ID，更新授权能力清单")
    public ApiResult<Object> updateActionsByMenuOfRole(
            @PathVariable Long id,
            @PathVariable Long menuid,
            @RequestBody Set<Long> permissionIds
    ) {
        sysRolePermissionService.saveAllByMenu(id, menuid, permissionIds);
        return ApiResult.ok();
    }
}
